package com.bsg.upm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.cache.DefinitionMetricCache;
import com.bsg.upm.constant.MonitorCommonMap;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.DictService;
import com.bsg.upm.service.MonitorHostConfigService;
import com.bsg.upm.service.MonitorHostService;

/**
 * 
 * @author yucs
 *
 */

@Controller
@RequestMapping("/v1.0/monitor/hosts")
public class MonitorHostController {
	@Resource
	private MonitorHostService monitorHostService;

	@Resource
	private DictService dictService;

	@Resource
	private DefinitionMetricCache definitionMetricCache;

	@Resource
	private MonitorHostConfigService monitorHostConfigService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @return 操作结果
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listHost(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			searchParamMap.put("clusterId", paramMap.get("cluster_id"));
			respJson = monitorHostService.list(searchParamMap);

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	@RequestMapping(value = "/{host_id}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getHostDetail(@PathVariable("host_id") String hostId, HttpServletResponse response) {

		RespJson respJson = null;

		try {
			Map<String, Object> paramMap = new HashMap<String, Object>();
			paramMap.put("hostId", hostId);
			respJson = monitorHostService.getHostDetail(paramMap);

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;

	}

	@RequestMapping(value = "{host_id}/{metric}", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getHostMetric(@PathVariable("host_id") String hostId, @PathVariable("metric") String metric,
			@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {

		RespJson respJson = null;

		if (!definitionMetricCache.isExist(MonitorCommonMap.HOST_TYPE, metric)) {
			response.setStatus(HttpStatus.SC_BAD_REQUEST);
			return RespJsonFactory.build(HttpStatus.SC_BAD_REQUEST, "don't the find the metric");
		}

		try {
			paramMap.put("hostId", hostId);
			paramMap.put("metric", metric);

			if (paramMap.containsKey("start_time") && paramMap.containsKey("end_time")) {
				respJson = monitorHostService.historyHandle(paramMap);
			} else {
				respJson = monitorHostService.noHistoryHandle(paramMap);
			}

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;

	}

	@RequestMapping(value = "threshold", method = RequestMethod.GET)
	@ResponseBody
	public RespJson getThreshold(HttpServletResponse response) {
		RespJson respJson = null;

		try {
			respJson = monitorHostService.getThreshold();
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}

	@RequestMapping(value = "threshold", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson getThreshold(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;

		Integer value = (Integer) paramMap.get("value");

		try {
			respJson = monitorHostService.updateThreshold(value);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}

		return respJson;
	}

	// 主机监控配置   以下,hosts必须同站点
	
	
	@RequestMapping(value = "config/event/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");

			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));

			respJson = monitorHostConfigService.enableEvent(siteId, metric, hosts);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	@RequestMapping(value = "config/event/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableEvent(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");

			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));

			respJson = monitorHostConfigService.disableEvent(siteId, metric, hosts);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	@RequestMapping(value = "config/metricclass/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableMetricClass(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric_class");

			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));

			respJson = monitorHostConfigService.enableMetricClass(siteId, metric, hosts);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	@RequestMapping(value = "config/metricclass/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableMetricClass(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric_class");

			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));
			
			respJson = monitorHostConfigService.disableMetricClass(siteId, metric, hosts);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	@RequestMapping(value = "config/event/level/enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enableEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String level =(String) paramMap.get("level");
			
			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));

			respJson = monitorHostConfigService.enableEventLevel(siteId, metric,level, hosts);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	
	@RequestMapping(value = "config/event/level", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson updateEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String level =(String) paramMap.get("level");
			String start =(String) paramMap.get("start");
			String end =(String) paramMap.get("end");
			
			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));

			respJson = monitorHostConfigService.updateEvent(siteId,hosts, metric,level, start,end);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	@RequestMapping(value = "config/event/level/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disableEventLevel(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			String metric = (String) paramMap.get("metric");
			String level =(String) paramMap.get("level");
			
			@SuppressWarnings("unchecked")
			ArrayList<String> hosts = (ArrayList<String>) paramMap.get("hosts");
			if (hosts.isEmpty()) {
				return RespJsonFactory.buildOK();
			}

			String siteId = monitorHostConfigService.getSiteIdByHostId(hosts.get(0));

			respJson = monitorHostConfigService.disableEventLevel(siteId, metric,level, hosts);
			response.setStatus(respJson.getStatus());

		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	

	@RequestMapping(value = "config/{host_id}/metricclass-infos", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listHostMetricClass(@PathVariable("host_id") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = monitorHostConfigService.listHostMetricClass(hostId);
			response.setStatus(respJson.getStatus());
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	@RequestMapping(value = "config/{host_id}/event-infos", method = RequestMethod.GET)
	@ResponseBody
	public RespJson listHostEvent(@PathVariable("host_id") String hostId, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = monitorHostConfigService.listHostEvent(hostId);
			response.setStatus(respJson.getStatus());
		} catch (Exception e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}
	
	
	

}
